package com.sky.service.impl;

import com.sky.dto.DishReviewDTO;
import com.sky.entity.DishReview;
import com.sky.entity.Orders;
import com.sky.mapper.DishReviewMapper;
import com.sky.mapper.OrderMapper;
import com.sky.result.Result;
import com.sky.service.DishReviewService;
import com.sky.vo.DishReviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品评价服务实现类
 */
@Slf4j
@Service
public class DishReviewServiceImpl implements DishReviewService {

    @Autowired
    private DishReviewMapper dishReviewMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 添加菜品评价
     * @param dishReviewDTO 菜品评价DTO
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> addReview(DishReviewDTO dishReviewDTO, Long userId) {
        log.info("添加菜品评价：{}, 用户ID：{}", dishReviewDTO, userId);
        // 1. 检查订单是否存在且已完成
        Orders orders = orderMapper.getById(dishReviewDTO.getOrderId());

        if (orders == null || orders.getStatus() != 5) {
            log.info("zhe44444");
            return Result.error("订单不存在或未完成");
        }

        // 2. 检查用户是否已评价过该菜品
        int count = dishReviewMapper.countByOrderIdAndDishIdAndUserId(
                dishReviewDTO.getOrderId(), dishReviewDTO.getDishId(), userId);
        if (count > 0) {
            log.info("zhe55555");
            return Result.error("您已经评价过该菜品了");
        }

        // 3. 构建评价对象
        DishReview dishReview = new DishReview();
        dishReview.setOrderId(dishReviewDTO.getOrderId());
        dishReview.setOrderDetailId(dishReviewDTO.getOrderDetailId());
        dishReview.setDishId(dishReviewDTO.getDishId());
        dishReview.setUserId(userId);
        dishReview.setRating(dishReviewDTO.getRating());
        dishReview.setContent(dishReviewDTO.getContent());

        // 4. 处理图片
        if (dishReviewDTO.getImageList() != null && !dishReviewDTO.getImageList().isEmpty()) {
            String images = String.join(",", dishReviewDTO.getImageList());
            dishReview.setImages(images);
        }

        // 5. 设置时间和状态
        dishReview.setCreateTime(LocalDateTime.now());
        dishReview.setStatus(1); // 默认显示

        // 6. 保存评价
        dishReviewMapper.insert(dishReview);


        return Result.success("评价成功");
    }

    /**
     * 根据订单ID查询用户已评价的菜品列表
     * @param orderId 订单ID
     * @param userId 用户ID
     * @return 评价列表
     */
    @Override
    public Result<List<DishReviewVO>> getReviewsByOrderId(Long orderId, Long userId) {
        List<DishReviewVO> reviewList = dishReviewMapper.selectByOrderIdAndUserId(orderId, userId);

        log.info("{}",reviewList);
        // 处理图片列表
        processImageList(reviewList);

        return Result.success(reviewList);
    }

    /**
     * 根据菜品ID查询评价列表
     * @param dishId 菜品ID
     * @return 评价列表
     */
    @Override
    public Result<List<DishReviewVO>> getReviewsByDishId(Long dishId) {
        List<DishReviewVO> reviewList = dishReviewMapper.selectByDishId(dishId);

        // 处理图片列表
        processImageList(reviewList);

        return Result.success(reviewList);
    }

    /**
     * 查询订单中未评价的菜品列表
     * @param orderId 订单ID
     * @param userId 用户ID
     * @return 未评价的菜品列表
     */
    @Override
    public Result<List<DishReviewVO>> getUnreviewedDishByOrderId(Long orderId, Long userId) {
        // 查询订单状态是否为已完成(status = 5)
        Orders orders = orderMapper.getById(orderId);
        if (orders == null || orders.getStatus() != 5) {
            return Result.error("订单不存在或未完成");
        }

        List<DishReviewVO> unreviewedList = dishReviewMapper.selectUnreviewedDishByOrderId(orderId, userId);
        return Result.success(unreviewedList);
    }

    /**
     * 处理图片列表
     * @param reviewList 评价列表
     */
    private void processImageList(List<DishReviewVO> reviewList) {
        if (reviewList != null && !reviewList.isEmpty()) {
            for (DishReviewVO vo : reviewList) {
                if (vo.getImages() != null && !vo.getImages().isEmpty()) {
                    List<String> imageList = Arrays.asList(vo.getImages().split(","));
                    vo.setImageList(imageList);
                }
            }
        }
    }
}